<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click.native="addfn"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table v-loading="loading" :data="list">
              <el-table-column label="序号" width="100" type="index" :index="totalIndex" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="updatafn(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click.native="delfn(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              :current-page="page"
              :page-sizes="[2 , 5, 10, 15]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyForm.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyForm.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyForm.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyForm.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-dialog :title="showtitle" :visible="showDialog" @close="closeDialog">
      <el-form ref="roleForm" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入角色名称" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="form.description" placeholder="请输入角色描述" />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="addrequed">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { getUserInfoAPI, deleteUserInfoAPI, AddUserInfoAPI, getUserAPI, UpdateUserInfoAPI, getID } from '@/api/setting'
export default {
  name: 'Setting',
  data() {
    return {
      page: 1,
      pagesize: 5,
      list: [],
      total: 0,
      loading: false,
      showDialog: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: ['blur', 'change'] }
        ]
      },
      companyForm: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      }
    }
  },
  computed: {
    totalIndex() {
      return (this.page - 1) * this.pagesize + 1
    },
    /**
     * 更换title
     */
    showtitle() {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.UserList()
    this.getId()
  },
  methods: {
    /**
     * 获取用户角色
     */
    async UserList() {
      this.loading = true
      const res = await getUserInfoAPI({
        page: this.page,
        pagesize: this.pagesize
      })
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
      this.loading = false
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.UserList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.UserList()
    },
    /**
     * 删除功能
     */
    delfn(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteUserInfoAPI(id)
        console.log(res)
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        this.UserList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /**
     * 弹层关闭
     */
    closeDialog() {
      this.showDialog = false
      this.$refs.roleForm.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    addfn() {
      this.showDialog = true
    },
    addrequed() {
      this.$refs.roleForm.validate(async(flag) => {
        if (flag) {
          if (this.form.id) {
            const res = await UpdateUserInfoAPI(this.form)
            console.log(res)
            this.$message.success('修改成功')
          } else {
            const res = await AddUserInfoAPI(this.form)
            console.log(res)
            this.$message.success('添加成功')
          }
          this.closeDialog()
          this.UserList()
        }
      })
    },
    /**
     * 获取角色详情
     */
    async updatafn(id) {
      this.showDialog = true
      const res = await getUserAPI(id)
      console.log(res)
      this.form = res.data
    },
    async  getId() {
      const id = this.$store.state.user.userInfo.companyId
      const res = await getID(id)
      console.log(res)
      this.companyForm = res.data
    }
  }
}
</script>

<style>

</style>
